using System.Collections;
using System.Collections.Generic;
using System.Windows.Browser;

namespace HtmlUtilities
{
    public interface IScriptObjectCollection : IScriptObject, IEnumerable<IScriptObject>
    {
        int Count { get; }
        IScriptObject this[int index] { get; }
    }

    internal class ScriptObjectCollectionWrapper : ScriptObjectWrapper, IScriptObjectCollection
    {
        private readonly ScriptObjectCollection _collection;
        public ScriptObjectCollectionWrapper(ScriptObjectCollection collection) : base(collection)
        {
            _collection = collection;
        }

        private class EnumeratorWrapper : IEnumerator<IScriptObject>
        {
            private readonly IEnumerator<ScriptObject> _source;
            public EnumeratorWrapper(IEnumerator<ScriptObject> source)
            {
                _source = source;
            }

            public void Dispose() { _source.Dispose(); }
            public bool MoveNext() { return _source.MoveNext(); }
            public void Reset() { _source.Reset(); }
            public IScriptObject Current { get { return ScriptObjectFactory.GetScriptObject(_source.Current); } }
            object IEnumerator.Current { get { return Current; } }
        }

        public IEnumerator<IScriptObject> GetEnumerator()
        {
            return new EnumeratorWrapper((_collection as IEnumerable<ScriptObject>).GetEnumerator());
        }

        IEnumerator IEnumerable.GetEnumerator()
        {
            return GetEnumerator();
        }

        public int Count
        {
            get { return _collection.Count; }
        }

        public IScriptObject this[int index]
        {
            get { return ScriptObjectFactory.GetScriptObject(_collection[index]); }
        }
    }
}